mui.init({
	keyEventBind: {
		backbutton: false,
		menubutton: true
	}
});

mui.plusReady(function() {
	var ws = plus.webview.currentWebview(),
		user_id = plus.storage.getItem('cg_user_id'),
		user_token = plus.storage.getItem('cg_user_token'),
		friend_list = [],
		friend_list_wrap = {};
	//安卓设置回弹效果
	ws.setBounce({
		position: {
			top: '344px'
		},
		changeoffset: {
			top: '0'
		}
	});
	getList();
	setTimeout(function() {
		var list = document.getElementById('list');
		window.indexedList = new mui.IndexedList(list);
		list.style.height = (document.body.offsetHeight - 44) + 'px';
	}, 1000);
	window.addEventListener('refresh', function(event) {
		getList(); //通过event.detail可获得传递过来的参数内容
	});
	//点击添加好友跳转到friend_add_header.html
	$('.friend-add').on('tap', function(e) {
		e.preventDefault();
		mui.openWindow({
			url: 'friend_add_header.html',
			show: {
				autoShow: false,
				aniShow: 'slide-in-right',
				duration: '100'
			}
		});
	});
	//搜索框获得焦点显示取消按钮
	$('.input-click').focus(function(e) {
		e.preventDefault();
		$('.mui-search').css({
			'width': '80%'
		});
		$('.s-click').show();
	}).blur(function() {
		$('.mui-search').css({
			'width': '100%'
		});
		$('.s-click').hide();
	});
	//点击取消按钮隐藏取消按钮
	$('.s-click').on('touchstart', function(e) {
		e.preventDefault();
		$('.mui-search').css({
			'width': '100%'
		});
		$('.input-click').blur();
		$('.s-click').hide();
	});
	//点击列表项跳转到详细页面
	$('.address-list').delegate('.address-item', 'tap', function() {
		var friend_id = $(this).attr('id');
		plus.storage.setItem('temp_friend_id', friend_id);
		mui.openWindow({
			url: 'friend_info_header.html',
			show: {
				autoShow: false, //页面loaded事件发生后自动显示，默认为true
				aniShow: 'slide-in-right', //页面显示动画，默认为”slide-in-right“；
				duration: '200' //页面动画持续时间，Android平台默认100毫秒，iOS平台默认200毫秒；
			},
			extras: {
				'friend_id': friend_id
			}
		});
	});
	//通讯录列表渲染函数
	function getList() {
		friend_list.length = 0;
		myAjax('addresslist/front_user_relation/getuserrealtion', 'get', {
			'user_id': user_id,
			'user_token': user_token
		}, function(data) {
			if(data.success) {
				delete data.success;
				$('.address-item').remove();
				$('.mui-table-view-divider').remove();
				$.each(data, function(key, value) {
					friend_list.push({
						'letter': key,
						'friendinfo': value
					});
				});
				friend_list_wrap.friend_list = friend_list;
				friend_list_wrap.baseurl = _baseUrl;
				var html = template('friend-list', friend_list_wrap);
				$('.address-list').append(html);
			} else {
				$('.mui-indexed-list-bar').hide();
				$('.mui-text-center').remove();
				$('.mui-indexed-list-inner').append('<h6 class="mui-text-center">获取好友列表失败，请重试</h1>');
			}
		});
	}
});